package com.oceansoft.core.common.util.impexp4xls;

import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

public class BuildXlsConfig {

	public static Cell buildConfig(@SuppressWarnings("rawtypes") Map Custom, Sheet sheet, Cell cell,
			CellStyle thstyle, Object[] cols,Map<String, Object> fieldMapping) {
		Row row;
		if ("1".equals(Custom.get("isCreateColum"))) {
			int personal_cols = 0;
			if (null != Custom.get("personal_cols")) {
				personal_cols = (Integer) Custom.get("personal_cols");
			}
			int units_cols = 0;
			if (null != Custom.get("units_cols")) {
				units_cols = (Integer) Custom.get("units_cols");
			}

			if (personal_cols > 0) {
				row = sheet.getRow(0);
				cell = row.createCell(2);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue("个人");
				sheet.addMergedRegion(new CellRangeAddress(0, 0, 2,
						1 + personal_cols));
				cell.setCellStyle(thstyle);
			}

			if (units_cols > 0) {
				row = sheet.getRow(0);
				cell = row.createCell(2 + personal_cols);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue("集体");
				sheet.addMergedRegion(new CellRangeAddress(0, 0,
						2 + personal_cols, 1 + personal_cols + units_cols));
				cell.setCellStyle(thstyle);
			}

			for (int j = 1; j < cols.length; j++) {
				String colname = cols[j].toString().split("_")[0];
				// 第2行
				row = sheet.getRow(1);
				cell = row.createCell(j);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(colname);
			}
			
			
			
			for(int k =1;k<cols.length;k++)
			{
				Map colMap = new HashMap();
				colMap.put("fieldName", cols[k].toString());
				colMap.put("labelName", cols[k].toString());
				fieldMapping.put(k+"",colMap);
			}
			
		}
		else if ("2".equals(Custom.get("isCreateColum"))) {


			for (int j = 3; j < cols.length; j++) {
				String colname = cols[j].toString().split("_")[0];
				// 第1行
				row = sheet.getRow(0);
				cell = row.createCell((j-1)*2-1);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				sheet.addMergedRegion(new CellRangeAddress(0, 0, (j-1)*2-1,
						(j-1)*2));
				cell.setCellValue(colname);
				
				
				row = sheet.getRow(1);
				cell = row.createCell((j-1)*2-1);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue("人数");
				
				cell = row.createCell((j-1)*2);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue("占比");
				
				
			}
			
			
			//重构数据列
			for(int k =1;k<cols.length;k++)
			{
				Map colMap = new HashMap();
				colMap.put("fieldName", cols[k].toString());
				colMap.put("labelName", cols[k].toString());
				System.out.println(k+"  "+colMap);
				if(k>=3)
				{
					fieldMapping.put( ((k-3)*2+3 ) +""   ,colMap);//33, 45 57 69
				}
				else
				{
					fieldMapping.put(k+"",colMap);
				}
			}
		}
		return cell;
	}
}
